// detail.js
import { getVolunteerDetail } from '../../../utils/volunteer';
Page({
  data: {
    volunteer: null,
    volunteerId: '',
    isLoading: true
  },

  onLoad: function (options) {
    console.log(options);
    if (options.id) {
      this.setData({
        volunteerId: options.id
      });
      this.loadVolunteerDetail(options.id);
    } else {
      wx.showToast({
        title: '未指定志愿者',
        icon: 'error',
        duration: 2000
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 2000);
    }
  },

  // 加载志愿者详细信息
  loadVolunteerDetail: function (id) {
    wx.showLoading({
      title: '加载中...',
    });

    // 这里应该是从API获取数据，这里模拟一些数据
    setTimeout(() => {
      getVolunteerDetail(id).then(res => {
        // console.log(res);
        const volunteer = res;
        this.setData({
          volunteer: volunteer,
          isLoading: false
        });
      });
      wx.hideLoading();
    }, 800);
  },

  // 编辑志愿者信息
  editVolunteer: function () {
    wx.navigateTo({
      url: '../add/add?id=' + this.data.volunteerId
    });
  },
  
  // 查看服务记录
  viewServiceRecords: function () {
    wx.navigateTo({
      url: '../viewServiceRecords/viewServiceRecords?volunteerId=' + this.data.volunteerId
    });
  },

  // 删除志愿者
  deleteVolunteer: function () {
    wx.showModal({
      title: '确认删除',
      content: '确定要删除该志愿者信息吗？此操作不可恢复。',
      confirmColor: '#FF4D4F',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '删除中...',
          });

          // 这里应该是调用API删除数据，这里模拟删除过程
          setTimeout(() => {
            wx.hideLoading();
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            });

            // 删除成功后返回上一页
            setTimeout(() => {
              wx.navigateBack();
            }, 2000);
          }, 1000);
        }
      }
    });
  },

  // 拨打电话
  callPhone: function () {
    if (this.data.volunteer && this.data.volunteer.phone) {
      wx.makePhoneCall({
        phoneNumber: this.data.volunteer.phone,
        fail(err) {
          console.error('拨打电话失败', err);
        }
      });
    } else {
      wx.showToast({
        title: '电话号码无效',
        icon: 'none'
      });
    }
  },

  // 预览照片
  previewPhoto: function () {
    if (this.data.volunteer && this.data.volunteer.photo) {
      wx.previewImage({
        urls: [this.data.volunteer.photo],
        current: this.data.volunteer.photo
      });
    } else {
      wx.showToast({
        title: '无照片可预览',
        icon: 'none'
      });
    }
  },

  // 复制微信号
  copyWechat: function () {
    if (this.data.volunteer && this.data.volunteer.wechat) {
      wx.setClipboardData({
        data: this.data.volunteer.wechat,
        success: function () {
          wx.showToast({
            title: '微信号已复制',
            icon: 'success'
          });
        }
      });
    }
  },

  // 复制邮箱
  copyEmail: function () {
    if (this.data.volunteer && this.data.volunteer.email) {
      wx.setClipboardData({
        data: this.data.volunteer.email,
        success: function () {
          wx.showToast({
            title: '邮箱已复制',
            icon: 'success'
          });
        }
      });
    }
  },

  // 返回上一页
  goBack: function () {
    wx.navigateBack();
  }
});